/*
    Down In Front: The Android Application
    This application is an interface for Down In Front commentaries.
    Copyright (C) 2011
    Holden Hill <holdenhill@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package down.in.front;

import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Adapts the custom layout fot the archives into a list view.
 * 
 * @author Holden Hill <holdenhill@gmail.com>
 * 
 */
public class ArchiveAdapter extends BaseAdapter {
	private Context context;
	private List<Episode> episodeList;

	/**
	 * Returns the size of the episode list.
	 */
	@Override
	public int getCount() {
		Log.i("LIST SIZE", "" + episodeList.size());
		return episodeList.size();
	}

	/**
	 * Create the archive adapter.
	 * 
	 * @param context
	 *            The context for the application.
	 * @param episodeList
	 *            The list of episode data.
	 */
	public ArchiveAdapter(Context context, List<Episode> episodeList) {
		this.context = context;
		this.episodeList = episodeList;
	}

	/**
	 * Unused.
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Episode temp = episodeList.get(getCount() - position - 1);
		Typeface tf = Typeface.createFromAsset(this.context.getAssets(),
				"fonts/WHOOPASS.TTF");

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.list, null);
		}

		// Sets the poster image.
		ImageView episodeImage = (ImageView) convertView
				.findViewById(R.id.ArchiveImage);
		episodeImage.setImageDrawable(LoadImage(temp.getPosterLink()));

		// Sets the episode title + typeface.
		TextView episodeTitle = (TextView) convertView
				.findViewById(R.id.ArchiveItem);
		episodeTitle.setText(temp.getTitle());
		episodeTitle.setTypeface(tf);

		// Sets the commentators.
		TextView commentators = (TextView) convertView
				.findViewById(R.id.ArchiveCommentators);
		commentators.setText(temp.getCommentators());
		Log.i("CURRENT TITLE", temp.getTitle());

		return convertView;
	}

	/**
	 * Creates a drawable image based on internal file locations.
	 * 
	 * @param filePath
	 *            A String containing the path to the image.
	 * @return An image to be drawn on the UI.
	 */
	private Drawable LoadImage(String filePath) {
		try {
			Drawable d = Drawable.createFromPath(filePath);

			return d;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * Unused.
	 */
	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}
}